<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
            <div class="form-group">
                <label class="col-md-2 control-label">员工</label>
                <div class="col-md-10" style="width: 300px">
                    <select class="form-control input-sm" name="userId" id="userId"
                            data-bv-notempty="true" data-bv-notempty-message="员工不能为空">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">签订时间</label>
                <div class="col-md-10" style="width: 200px">
                    <input type="text" class="form-control" placeholder="签订时间" id="signDate" name="signDate"
                           data-bv-notempty="true" data-bv-notempty-message="签订时间不能为空">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">到期时间</label>
                <div class="col-md-10" style="width: 200px">
                    <input type="text" class="form-control" placeholder="到期时间" id="expireDate" name="expireDate"
                           data-bv-notempty="true" data-bv-notempty-message="到期时间不能为空">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">合同图片</label>
                <div class="col-md-10">
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1">
                        <p id="demoText"></p>
                    </div>
                    <input type="hidden" id="url" name="url"/>
                </div>
            </div>
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='contractList.html'">返回</button>
                        <button class="btn btn-primary" type="submit" onclick="add()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap/bootstrap-select.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/my/roles/role.js"></script>
<script type="text/javascript" src="../../layui/layui.all.js?v=1.0.1"></script>
<script type="text/javascript" src="../../js/dict.js"></script>
<script type="text/javascript" src="../../js/my/queryData.js?v=2"></script>
<script type="text/javascript">
    layui.use(['layer', 'laydate', 'upload'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        var upload = layui.upload;
        laydate.render({
            elem: '#signDate'
        });
        laydate.render({
            elem: '#expireDate'
        });

        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/files' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                $("#url").val(res.url);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        initEmps("userId");
    });

    $('#form').bootstrapValidator();

    function add() {
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }
        var url = $("#url").val();
        if (!url || url == undefined || url == "") {
            layer.msg("请上传合同图片");
            return;
        }
        var formdata = $("#form").serializeObject();
        $.ajax({
            type: 'post',
            url: '/empContract',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                    location.href = "contractList.html";
                });
            }
        });
    }
</script>
</body>
</html>